import os
import random

import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'travel_recommend.settings')
django.setup()
from user.serializers import User, UserModelSerializer, UserComments, UserCommentsSerializer
from travel.serializers import Travel, TravelModelSerializer
import pandas as pd
import numpy as np


def mock_user():
    for i in range(1, 20):
        mock_data = {
            'username': "user%s" % i,
            'password': '123',
            'name': '普通用户%s' % i,
            'age': 18,
            'gender': '保密',
        }
        ser = UserModelSerializer(data=mock_data)
        if ser.is_valid():
            ser.save()
            print("%s添加成功" % mock_data['name'])
        else:
            print("添加失败", ser.errors)


def mock_user_comments():
    users = pd.DataFrame(list(User.objects.all().values()))
    travels = pd.DataFrame(list(Travel.objects.all().values()))

    top_travels = travels.sort_values(by='comment_total', ascending=False).iloc[:10, :1]['id']

    # userIds = users.iloc[:5, :1].values.tolist()
    # ids = [id[0] for id in userIds]

    # travelIds = random.sample(top_travels, 10)
    userIds = users['id'].values.tolist()

    for id in list(top_travels):
        userId = random.sample(userIds, 1)[0]
        data = {
            'user': userId,
            'travel': id,
            'score': random.randint(1, 5)
        }
        ser = UserCommentsSerializer(data=data)
        if ser.is_valid():
            ser.save()
            print("%s添加成功" % id)
        else:
            print("%s添加失败" % ser.errors)


if __name__ == '__main__':
    # mock_user()
    mock_user_comments()
